package com.ly.lypopupwindowdemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.ly.lypopupwindowdemo.MessageCenterBean;
import com.ly.lypopupwindowdemo.OnItemListener;
import com.ly.lypopupwindowdemo.R;

import java.util.HashSet;
import java.util.List;

/**
 * Created by liying on 2017/4/20.
 */

public class MessageCenterGGAdapter extends RecyclerView.Adapter<MessageCenterGGAdapter.GGViewHolder> {
    private Context mContext;
    private List<MessageCenterBean> lists;

    private OnItemListener mOnItemListener;

    boolean isEdit;
    //boolean isAllCheck;
    int isAllCheck = -1;
    boolean isAllRead;

    //private List<Integer> checkPositionList;
    private HashSet<Integer> positionSet;

    public MessageCenterGGAdapter(Context mContext, List<MessageCenterBean> lists, HashSet<Integer> positionSet) {
        this.mContext = mContext;
        this.lists = lists;
        //checkPositionList = new ArrayList<>();
        this.positionSet = positionSet;
    }

    @Override
    public GGViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_message_center_gg, parent, false);
        GGViewHolder holder = new GGViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final GGViewHolder holder, final int position) {

        final MessageCenterBean bean = lists.get(position);

        holder.title.setText(bean.getTitle());

        if (bean.isRead()) {
            holder.red_point.setVisibility(View.GONE);
        } else {
            holder.red_point.setVisibility(View.VISIBLE);
        }

////////////////////////////////////
        Log.e("isSelected", position + "个bean.isSelect()" + bean.isSelect());


        holder.item_check.setChecked(bean.isSelect());
        holder.item_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //bean.isSelect=holder.item_check.isChecked();
                //   lists.get(position).setSelect(true);
                bean.setSelect(holder.item_check.isChecked());
                mOnItemListener.checkBoxClick(position);
            }
        });


        holder.right_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemListener.onItemClick(v, position);
            }
        });


        if (isEdit) {
            //如果正在编辑
            holder.item_check.setVisibility(View.VISIBLE);

        } else {
            holder.item_check.setVisibility(View.GONE);
        }

        if (isAllRead) {
            //全选设为已读
//            if (holder.red_point.getVisibility()==View.GONE){
//                ToastUtil.showShortToast("已经全部设为已读");
//            }
            //TODO 需要链接接口通知全部设置为已读状态
            holder.red_point.setVisibility(View.GONE);


        } else {

            //判断当前有没有选中的item
        }




    }

    @Override
    public int getItemCount() {
        return lists.size();
    }



    public class GGViewHolder extends RecyclerView.ViewHolder {

        private CheckBox item_check;
        private TextView red_point;
        private LinearLayout right_ll;
        private TextView title;

        //private ImageView check_iv;

        public GGViewHolder(View itemView) {
            super(itemView);
            item_check = (CheckBox) itemView.findViewById(R.id.id_item_checkbox);
            red_point = (TextView) itemView.findViewById(R.id.id_red_point);
            right_ll = (LinearLayout) itemView.findViewById(R.id.id_right_ll);
            title = (TextView) itemView.findViewById(R.id.id_title);

            // check_iv = (ImageView) itemView.findViewById(R.id.id_item_iv);
        }
    }


    public void isEdit(boolean isEdit) {
        this.isEdit = isEdit;
    }


    public boolean isAllRead() {
        return isAllRead;
    }

    public void setAllRead(boolean allRead) {
        isAllRead = allRead;
    }

    public void setOnItemListener(OnItemListener onItemListener) {
        this.mOnItemListener = onItemListener;
    }

    public MessageCenterBean getItem(int pos) {
        return lists.get(pos);
    }

    public void remove(MessageCenterBean bean) {
        lists.remove(bean);
    }

//    public int getIsAllCheck() {
//        return isAllCheck;
//    }
//
//    public void setIsAllCheck(int isAllCheck) {
//        this.isAllCheck = isAllCheck;
//    }
}
